package edu.towson.cosc.cosc455.ddange1.markdowncompiler;
import java.io.IOException;
import java.util.*;

public class mySemanticAnalyzer {
	public Stack<String> copy = new Stack<String>();
	public String string = "";
	public String name = "";
	//go through the stack and make sure there is a 
		//#end for every #start
	//transform to html and send to document
	public void convert(){
		while(!compiler.info.isEmpty()){
			string = compiler.info.pop();
			copy.push(string);
		}
		
		while(!copy.isEmpty()){
			string = copy.pop();
			//go through each token and run check on it
			try{
				compiler.writer.write(check(string));
			}
			catch(IOException e){
				e.printStackTrace();
			}
			compiler.info.push(string);
		}
	}
	public String check(String s){
		String html = "";
		if(s.equalsIgnoreCase("#START DOCUMENT")){
			html = "<html>\n";
		}
		else if(s.equalsIgnoreCase("#END DOCUMENT")){
			html = "\n</html>";
		}
		else if(s.equalsIgnoreCase("#START HEAD")){
			html = "<head>\n";
		}
		else if(s.equalsIgnoreCase("#END HEAD")){
			html = "\n</head>";
		}
		else if(s.equalsIgnoreCase("#START TITLE")){
			html = "<title>\n";
		}
		else if(s.equalsIgnoreCase("#END TITLE")){
			html = "\n</title>\n";
		}
		else if(s.equalsIgnoreCase("#START PARAGRAPH")){
			html = "<p>\n";
		}
		else if(s.equalsIgnoreCase("#END PARAGRAPH")){
			html = "\n</p>";
			deleteInnerFunction("#START PARAGRAPH");
		}
		else if(s.equalsIgnoreCase("#START BOLD")){
			html = "<b>";
		}
		else if(s.equalsIgnoreCase("#END BOLD")){
			html = "</b>";
		}
		else if(s.equalsIgnoreCase("#START ITALICS")){
			html = "<i>";
		}
		else if(s.equalsIgnoreCase("#END ITALICS")){
			html = "</i>";
		}
		else if(s.equalsIgnoreCase("#START LIST")){
			html = "<ul>\n";
		}
		else if(s.equalsIgnoreCase("#END LIST")){
			html = "</ul>\n";
		}
		else if(s.equalsIgnoreCase("#START ITEM")){
			html = "<li>";
		}
		else if(s.equalsIgnoreCase("#END ITEM")){
			html = "</li>\n";
		}
		else if(s.equalsIgnoreCase("#NEWLINE")){
			html = "<br>";
		}
		else if(s.equalsIgnoreCase("#START AUDIO")){
			html = "<audio controls> <source src=\" ";
		}
		else if(s.equalsIgnoreCase("#END AUDIO")){
			html = " \" > </audio>\n";
		}
		else if(s.equalsIgnoreCase("#START VIDEO")){
			html = "<iframe src=\""; 
		}
		else if(s.equalsIgnoreCase("#END VIDEO")){
			html = "\" />\n";
		}
		else if(s.equalsIgnoreCase("#START DEFINE")){
		}
		else if(s.equalsIgnoreCase("#END DEFINE")){
		}
		else if(s.equalsIgnoreCase("#NAME")){
		}
		else if(s.equalsIgnoreCase("#VALUE")){
		}
		else if(s.equalsIgnoreCase("#START USE")){
			name = copy.pop();
			compiler.info.push(name);
			html = checkUse(name);
		}
		else if(s.equalsIgnoreCase("#END USE")){		
		}
		else{ //text and spaces
			if(isText(s)){
			html = s;
			}
		}
		return html;
	}
	
	//error came up with printing the values
		//of a define tag. This creates the exception
	public boolean isText(String s){
		boolean isText = true;
		String temp = compiler.info.pop();
		compiler.info.push(temp);
		if((temp.equalsIgnoreCase("#Value")) || (temp.equalsIgnoreCase("#Name"))){
			isText = false;
		}
		return isText;
	}
	//deletes everything within a particular tag
	public void deleteInnerFunction(String tag){
		String temp = compiler.info.pop();
		while(!temp.equalsIgnoreCase(tag) && (!compiler.info.isEmpty())){
			temp = compiler.info.pop();
		}
	}
	
	//This finds the definition of the value
	public String checkUse(String someName){//returns the most recent definition of a given name
		String value = "";
		String[] newArray =	compiler.info.toArray(new String[0]);
		boolean found = false;
		for(int i = newArray.length-1; (i > 0) && (!found); i--){
			if(newArray[i].equalsIgnoreCase("#Name")){
				if(newArray[i+1].equalsIgnoreCase(someName)){
					value = newArray[i+3];
					found = true;
				}
			}
		}
		return value;
	}
}
